package views;

import bean.Cls;
import bean.Student;
import service.ClassService;
import utils.TableMode;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class StudentList extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTable table;
    private JTextField textField_1;
    Cls cls2=new Cls();
    Student student =new Student();
    public  void setCls(Cls cls) {
        cls2=cls;
        student.setC_id(cls2.getC_id());
        student.setS_id(textField.getText());
        student.setS_name(textField_1.getText());
        System.out.println(student);
        //查询小组列表
        System.out.println(cls2);
        ClassService s=new ClassService();
        List<Student> sList=s.selectStudent(student);
        Vector data =new Vector();
        Iterator<Student> iterator=sList.iterator();
        while(iterator.hasNext()){
            Student next = iterator.next();
            Vector row=new Vector();
            row.add(next.getS_id());
            row.add(next.getS_name());
            data.add(row);
        }

        Vector colNames=new Vector();
        colNames.add("学号");
        colNames.add("姓名");
        TableMode mode=new TableMode(data, colNames);
        table.setModel(mode);
    }
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StudentList frame = new StudentList();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public StudentList() {
        setTitle("小组列表");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 731, 594);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(257, 8, 133, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("查询");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ClassService s=new ClassService();
                //通过姓名查询
                if(textField_1.getText().length()==0) {
                    student.setS_name(textField.getText());
                    List<Student> sList=s.selectStudentBysname(student);
                    Vector data =new Vector();
                    Iterator<Student> iterator=sList.iterator();
                    while(iterator.hasNext()){
                        Student next = iterator.next();
                        Vector row=new Vector();
                        row.add(next.getS_id());
                        row.add(next.getS_name());
                        data.add(row);
                    }

                    Vector colNames=new Vector();
                    colNames.add("学号");
                    colNames.add("姓名");
                    TableMode mode=new TableMode(data, colNames);
                    table.setModel(mode);
                }

                //通过学号查询
                if(textField.getText().length()==0) {
                    student.setS_id(textField_1.getText());
                    List<Student> sList=s.selectStudentBysid(student);
                    Vector data =new Vector();
                    Iterator<Student> iterator=sList.iterator();
                    while(iterator.hasNext()){
                        Student next = iterator.next();
                        Vector row=new Vector();
                        row.add(next.getS_id());
                        row.add(next.getS_name());
                        data.add(row);
                    }

                    Vector colNames=new Vector();
                    colNames.add("学号");
                    colNames.add("姓名");
                    TableMode mode=new TableMode(data, colNames);
                    table.setModel(mode);
                }

                if(textField.getText().length()!=0&&textField_1.getText().length()!=0){
                    student.setS_name(textField.getText());
                    student.setS_id(textField_1.getText());
                    List<Student> sList=s.selectStudentByall(student);
                    Vector data =new Vector();
                    Iterator<Student> iterator=sList.iterator();
                    while(iterator.hasNext()){
                        Student next = iterator.next();
                        Vector row=new Vector();
                        row.add(next.getS_id());
                        row.add(next.getS_name());
                        data.add(row);
                    }

                    Vector colNames=new Vector();
                    colNames.add("学号");
                    colNames.add("姓名");
                    TableMode mode=new TableMode(data, colNames);
                    table.setModel(mode);
                }
            }
        });
        btnNewButton.setBounds(400, 8, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1_2 = new JButton("返回");
        btnNewButton_1_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Main m=new Main();
                m.setCls(cls2);
                m.setVisible(true);
                StudentList.this.dispose();
            }
        });
        btnNewButton_1_2.setBounds(503, 8, 93, 23);
        contentPane.add(btnNewButton_1_2);

        JButton btnNewButton_1_3 = new JButton("删除");
        btnNewButton_1_3.setBounds(606, 8, 93, 23);
        contentPane.add(btnNewButton_1_3);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 41, 689, 495);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);

        JLabel lblNewLabel = new JLabel("姓名：");
        lblNewLabel.setBounds(208, 12, 41, 15);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("学号：");
        lblNewLabel_1.setBounds(16, 12, 41, 15);
        contentPane.add(lblNewLabel_1);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(65, 8, 133, 21);
        contentPane.add(textField_1);



    }
}
